<!--结算申请审批明细-->
<template>
  <div>
    <f-search-form ref='search' :more='true' @searchHandler='searchHandler' style='margin-bottom: 10px' @exportHandler='exportHandler' :show-export-btn='true' :exportUrl='api.bhid.exportUrl'>
      <f-search-form-item label='订单编号' name='orderNo' />
      <f-search-form-item label='业务员' name='saleManName' />
      <f-search-form-item label='单位名称' name='customName' />
      <f-search-form-item label='部门' name='deptId' type='select' :data='api.hyorder.listByCompanyId' :params='queryParam' value-key='deptId' label-key='deptName' slot='advanced' />
      <f-search-form-item label='发货日期' type='range' name='beginDate' slot='advanced' />
    </f-search-form>
    <f-table ref='table'
             rowKey='invoiceDetailsId'
             :showSetting='true'
             setting-key='settleTab1'
             :tableColumns='columns'
             :scroll='{x:true}'
             :rowSelection='rowSelection'
             :showMultipleBtn='false'
             :showPagination='false'
             :showTotal='true'
             :multiple='multiple'
             handler='hySettlePayDetailsPageListHandler'
             :operator='operator'
             :autoLoad='false'
             :params='queryParam'
             @onSuccess='onSuccess'
             :delAction='api.bhid.remove'
             :api='api.bhid.list'>
      <span slot='drugType' slot-scope='text, record, index'>
           <a-tag color='#2db7f5' v-if='text=="ZY"'>自营</a-tag>
           <a-tag color='#FF0000' v-if='text=="ZS"'>招商</a-tag>
       </span>
      <span slot='sendType' slot-scope='text, record, index'>
           <a-tag color='#2db7f5' v-if='text==1'>现款</a-tag>
           <a-tag color='#FF0000' v-if='text==2'>预收款</a-tag>
           <a-tag color='#87d068' v-if='text==3'>资保金</a-tag>
           <a-tag color='purple' v-if='text==4'>全额资信</a-tag>
       </span>
      <span slot='action' slot-scope='text, record'>
         <template v-if='record.rowIndex!="合计"'>
            <a href='javascript:;' @click='$refs.orderDetailsDrawer.showDrawer(record)'>详情</a>
            <a-divider type='vertical' v-if='invoiceApply==1 && !record.settleInvoiceId '></a-divider>
            <a-divider type='vertical' v-if='invoiceData.deleteDetails==1'></a-divider>
            <a @click='openCfAmountModal(record)' v-if='!record.parentId && !record.settleInvoiceId && invoiceApply==1'>拆分</a>
            <a @click='revokeCfAmount(record)' v-if='record.parentId && !record.settleInvoiceId && invoiceApply==1'>撤销拆分</a>
            <a href='javascript:;' @click='clearPayId(record)' v-if='invoiceData.deleteDetails==1'>删除</a>
          </template>
        </span>
    </f-table>
    <a-divider orientation='left'>开票申请明细
    </a-divider>
    <f-table ref='invoiceTable' rowKey='paymentId' :operator='operator2' :showSetting='false' setting-key='invoice_table' :tableColumns='invoiceColumns' :scroll='{x:true}' handler='hyInvoicePaymentPageListHandler' :showPagination='false' :showMultipleBtn='false' :multiple='false' :params='queryParam2' :showTotal='true' :autoLoad='false' :api='api.payment' style='margin-bottom: 10px'>
       <span slot='invoiceType' slot-scope='text, record, index'>
            <a-tag color='#87d068' v-if='text==1'>普票</a-tag>
            <a-tag color='#108ee9' v-if='text==2'>专票</a-tag>
       </span>
      <span slot='paymentState' slot-scope='text, record, index'>
            <a-tag color='#87d068' v-if='text==0'>未支付</a-tag>
            <a-tag color='#108ee9' v-if='text==2'>已支付</a-tag>
       </span>
      <span slot='action' slot-scope='text, record' v-if='record.rowIndex!="合计"'>
          <template>
            <a @click='$refs.invoicePayDrawer.showDrawer(record)' v-if='invoiceData.invoicePay==1 && record.paymentState==0'>支付</a>
            <a-divider type='vertical' v-if='invoiceData.invoicePay==1 && record.paymentState==0' />
            <a @click='$refs.invoiceDetailsDrawer.showDrawer(record)'>明细</a>
            <a-divider type='vertical' v-if='!record.invoiceNo && invocieRecord.taskDefinitionKey=="Activity_ROLE_SW_HTZY"' />
            <a href='javascript:;' @click='$refs.fapiaoDrawer.showDrawer(record)' v-if='!record.invoiceNo && invocieRecord.taskDefinitionKey=="Activity_ROLE_SW_HTZY"'>发票</a>
            <a-divider type='vertical' />
          </template>
           <a-dropdown>
            <a class='ant-dropdown-link'>更多 <a-icon type='down' /></a>
            <a-menu slot='overlay'>
               <a-menu-item v-if='invocieRecord.taskDefinitionKey=="Activity_ROLE_SW_HTZY"'>
               <a href='javascript:;' @click="$refs.invoiceTable.del(record,{handlerName:'hyInvoicePaymentDeleteHandler',settleType:record.settleType},onLoad)" v-if='invoiceData.invoiceApply==1'>删除</a>
               </a-menu-item>
              <a-menu-item v-if='invocieRecord.taskDefinitionKey=="Activity_ROLE_SW_HTZY" || superAdmin'>
                  <a href='javascript:;' @click='editHandle(record)'>编辑</a>
              </a-menu-item>
              <a-menu-item v-if='invocieRecord.taskDefinitionKey=="Activity_ROLE_SW_HTZY" || superAdmin'>
                  <a href='javascript:;' @click='$refs.fapiaoDrawer.showDrawer(record)'>发票录入</a>
              </a-menu-item>
               <a-menu-item>
                <a href='javascript:;' @click='printOrder(record)'>开票申请单</a>
              </a-menu-item>
                <a-menu-item v-if='invocieRecord.taskDefinitionKey=="Activity_ROLE_SW_HTZY" || superAdmin'>
                <a href='javascript:;' @click='downloadKpsq(record)'>下载开票申请单</a>
              </a-menu-item>
                <a-menu-item v-if='invocieRecord.taskDefinitionKey=="Activity_ROLE_SW_HTZY" || superAdmin'>
                <a href='javascript:;' @click='downloadFwxy(record)'>下载服务协议</a>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>
    </f-table>
    <invoice-apply-drawer ref='invoiceApplyDrawer' @success='onLoad'></invoice-apply-drawer>
    <invoice-details-drawer ref='invoiceDetailsDrawer' :invoiceData='invoiceData' @success='onLoad'></invoice-details-drawer>
    <hy-order-details-drawer ref='orderDetailsDrawer'></hy-order-details-drawer>
    <print-modal ref='printModal'></print-modal>
    <invoice-pay-drawer ref='invoicePayDrawer' @success='onLoad'></invoice-pay-drawer>
    <a-modal title='拆分金额' v-model='visible' width='800px' height='500px' @cancel='this.visible=false' @ok='cfSubmit'>
      <f-form ref='cfForm' :action='api.bhid.cfAmount' v-if='visible'>
        <f-form-item label='拆分金额' type='number' :precision='2' :max='maxAmount' :value='maxAmount' data-type='number' name='amount' :required='true' />
      </f-form>
    </a-modal>
  </div>
</template>
<script>
import api from "./../../../js/api"
import InvoiceApplyDrawer from "./invoice/invoice_apply_drawer"
import InvoiceDetailsDrawer from "./invoice/invoice_details_drawer"
import HyOrderDetailsDrawer from "@/views/module/haiying/order/details/hyorder_details_drawer"
import PrintModal from "@/views/module/haiying/print/print_modal"
import InvoicePayDrawer from "@/views/module/haiying/tasktodo/payment/components/invoice/invoice_pay_drawer"

export default {
  name: "SettleDetails",
  components: { InvoicePayDrawer, PrintModal, HyOrderDetailsDrawer, InvoiceApplyDrawer, InvoiceDetailsDrawer },
  props: {
    columns: {
      type: Array,
      default: []
    },
    invoiceApply: {
      type: Number,
      default: null
    },
    invoiceData: {
      type: Object,
      default: null
    },
    multiple: {
      type: Boolean,
      default: true
    }
  },
  data() {
    let me = this
    //操作按钮
    let operator = []
    let operator2 = []
    if (this.invoiceApply == 1) {
      operator.push({
        name: "申请开票",
        that: this,
        handler: (table, that) => {
          let selectIds = table.selectedRowKeys
          if (selectIds.length > 0) {
            this.$refs.invoiceApplyDrawer.showDrawer({
              selectIds: selectIds.join(","),
              paymentId: this.queryParam.paymentId,
              settleType: this.queryParam.settleType
            })
          } else {
            that.$message.error("请选择开票数据")
          }
        }
      })
    }
    operator2.push({
      name: "导出",
      that: this,
      handler: (table, that) => {
        this.$refs.invoiceTable.tableExport()
      }
    })
    return {
      api: Object.assign(api, this.api),
      visible: false,
      maxAmount: 0,
      invoiceDetailsId: null,
      invoiceList: [],
      invocieRecord: {},
      //列表初始化查询参数
      queryParam: {
        fileName: "结算申请明细-",
        auth: 1,
        isFlp: 1
      },
      //列表初始化查询参数
      queryParam2: {
        fileName: "开票明细",
        auth: 1,
        isFlp: 1
      },
      returnCustomBond: 0,
      selectIds: "",
      params1: {
        deptId: ""
      },
      invoiceColumns: [
        {
          title: "序号",
          width: "60px",
          align: "center",
          customRender: (text, record, index) => index + 1
        },
        {
          title: "开票日期",
          align: "center",
          width: "100px",
          dataIndex: "amountDate"
        },
        {
          title: "单号",
          align: "center",
          width: "130px",
          dataIndex: "paymentNo"
        },
        {
          title: "发票项目",
          align: "left",
          ellipsis: true,
          width: "100px",
          dataIndex: "title"
        },
        {
          title: "发票类型",
          align: "center",
          width: "80px",
          dataIndex: "invoiceType",
          scopedSlots: { customRender: "invoiceType" }
        },
        {
          title: "收票单位",
          align: "left",
          width: "180px",
          ellipsis: true,
          dataIndex: "companyName"
        },
        {
          title: "开票单位",
          align: "left",
          ellipsis: true,
          width: "180px",
          dataIndex: "invoiceunitName"
        },
        {
          title: "结算金额",
          align: "left",
          width: "80px",
          needTotal: true,
          type: 10,
          dataIndex: "amount"
        },
        {
          title: "开票金额",
          align: "left",
          needTotal: true,
          width: "80px",
          type: 10,
          dataIndex: "invoiceAmount"
        },
        {
          title: "不含税金额",
          align: "left",
          width: "90px",
          type: 10,
          needTotal: true,
          dataIndex: "noTaxAmount"
        },
        {
          title: "税费",
          align: "left",
          width: "80px",
          needTotal: true,
          type: 10,
          dataIndex: "taxAmount"
        },
        {
          title: "3%开票金额",
          align: "left",
          needTotal: true,
          width: "100px",
          type: 10,
          dataIndex: "senondInvoiceAmount"
        },
        {
          title: "3%开票税金",
          align: "left",
          width: "100px",
          needTotal: true,
          type: 10,
          dataIndex: "secondTaxAmount"
        },
        {
          title: "备注",
          align: "left",
          width: "80px",
          ellipsis: true,
          dataIndex: "remark"
        }, {
          title: "申请人",
          align: "center",
          width: "90px",
          dataIndex: "nickName"
        }, {
          title: "创建时间",
          align: "center",
          width: "90px",
          dataIndex: "createTime"
        }, {
          title: "支付日期",
          align: "center",
          width: "90px",
          dataIndex: "payDate"
        }, {
          title: "支付状态",
          align: "center",
          width: "80px",
          fixed: "right",
          dataIndex: "paymentState",
          scopedSlots: { customRender: "paymentState" }
        }, {
          title: "操作",
          align: "center",
          width: "160px",
          dataIndex: "action",
          fixed: "right",
          scopedSlots: { customRender: "action" }
        }
      ],
      settleType: null,
      dataSource: [],
      operator: operator,
      operator2: operator2
    }
  },
  computed: {
    rowSelection() {
      if (this.multiple) {
        return {
          getCheckboxProps: record => ({
            props: {
              disabled: record.disabled
            }
          })
        }
      }
    }
  },
  methods: {
    setData(data) {
      if (data && data.paymentId) {
        this.invocieRecord = data
        this.queryParam.paymentId = data.paymentId
        this.queryParam2.paymentId = data.paymentId
        this.queryParam.settleType = data.settleType
        this.$refs["table"].refresh(false)
      }
    },
    onSuccess() {
      this.$refs["invoiceTable"].refresh(false)
    },
    //拆分金额提交
    cfSubmit() {
      this.$refs["cfForm"].submit({
        invoiceDetailsId: this.invoiceDetailsId
      }, () => {
        this.visible = false
        this.$refs["table"].refresh()
      })
    },
    //打开拆分金额弹框
    openCfAmountModal(record) {
      this.invoiceDetailsId = record.invoiceDetailsId
      this.maxAmount = record.settleApplyAmount
      this.visible = true
    },
    //撤销拆分
    revokeCfAmount(record) {
      this.http.confirm(this, {
        content: "确认撤销拆分吗?",
        url: api.bhid.revokeCfAmount,
        data: {
          invoiceDetailsId: record.invoiceDetailsId
        },
        success: (me, data) => {
          this.$refs.table.refresh()
        }
      })
    },
    //撤销支付
    clearPayId(record) {
      this.http.confirm(this, {
        content: "确认撤销该笔订单结算吗?",
        url: api.bhid.clearPayId,
        data: {
          invoiceDetailsId: record.invoiceDetailsId
        },
        success: (me, data) => {
          this.$refs.table.refresh()
        }
      })
    },
    /**
     * 查询事件
     * @param searchParams
     */
    searchTable(searchParams) {
      this.queryParam = Object.assign(this.queryParam, searchParams)
      this.$refs.table.refresh(true, searchParams)
    },
    printOrder(record) {
      this.$refs["printModal"].open(record.paymentId, 3, "Invoice")
    },
    editHandle(record) {
      this.$refs.invoiceApplyDrawer.showDrawer(record)
    },
    /**
     * 下载服务协议
     * @param record
     */
    downloadFwxy(record) {
      let env = process.env.NODE_ENV
      let url = "bus/export/fwxy"
      if (env == "development") {
        url = "../../../base/bus/export/fwxy"
      }
      this.http.exportExcel(this, {
        url: url,
        data: {
          paymentId: record.paymentId
        },
        success: function(me, data) {
        }
      })
      this.visible = false
    },
    /**
     * 下载开票申请
     * @param record
     */
    downloadKpsq(record) {
      let env = process.env.NODE_ENV
      let url = "bus/export/kpsq"
      if (env == "development") {
        url = "../../../base/bus/export/kpsq"
      }
      this.http.exportExcel(this, {
        url: url,
        data: {
          paymentId: record.paymentId
        },
        success: function(me, data) {
        }
      })
      this.visible = false
    },
    /**
     * 刷新列表
     */
    onLoad() {
      this.$refs["table"].refresh(false)
      this.$refs["invoiceTable"].refresh(false)
      this.$emit("changeTab")
    }
  },
  created() {
  }
}
</script>